const express = require('express');
const challengeController = require('../controllers/challengeController');
const { protect, restrictTo, optionalProtect } = require('../middlewares/authMiddleware');

const router = express.Router();

// 公开路由（可选保护，登录用户可以获取更多信息）
router.use(optionalProtect);
router.get('/', challengeController.getChallenges);

// 保护的路由（需要登录）
router.use(protect);

router.get('/recommended', challengeController.getRecommendedChallenges);
router.get('/learning-path', challengeController.getLearningPath);

router
  .route('/:id')
  .get(challengeController.getChallenge)
  .patch(challengeController.updateChallenge)
  .delete(challengeController.deleteChallenge);

router.post('/:id/submit', challengeController.submitChallenge);
router.post('/:id/rate', challengeController.rateChallenge);
router.post('/:id/hint', challengeController.unlockHint);

// 创建挑战（教师和管理员可用）
router.post('/', restrictTo('teacher', 'admin'), challengeController.createChallenge);

module.exports = router;